package com.wzd.learn.springcloud.service.a.biz;

import java.util.concurrent.locks.LockSupport;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class HelloServiceImpl implements HelloService {
	@GetMapping("/hello1")
	@HystrixCommand(fallbackMethod = "sayHello1Fallback")
	public String sayHello1(@RequestParam("name") String name) {
		System.out.println("request hello1.time start:" + System.currentTimeMillis());
		LockSupport.parkNanos(1000000000);
		System.out.println("request hello1.time end:" + System.currentTimeMillis());
		return "Hello1 " + name + "!";
	}

	public String sayHello1Fallback(String name) {
		System.out.println("hello1 fail!");
		return "Hello1 " + name + ",error!";
	}

	@GetMapping("/hello2")
	@HystrixCommand(fallbackMethod = "sayHello2Fallback")
	public String sayHello2(@RequestParam("name") String name) {
		System.out.println("request hello2.time:" + System.currentTimeMillis());
		int a = 1 / 0;
		System.out.println(a);
		return "Hello2 " + name + "!";
	}

	public String sayHello2Fallback(String name) {
		System.out.println("hello2 fail!");
		return "Hello2 " + name + ",error!";
	}
}
